package com.pet.taglib;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.views.jsp.StrutsBodyTagSupport;

import com.pet.framework.page.ATTPage;
import com.pet.framework.page.Page;
import com.pet.framework.util.StringUtil;
import com.pet.system.util.TemplateUtil;

import freemarker.template.TemplateException;

public class PageTag extends StrutsBodyTagSupport {
	private String id = null;
	private String templetNum = null;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	@Override
	public int doStartTag() throws JspException {
		String pageId = StringUtil.dealNull(id, ATTPage.PAGE_LIST);
		Page page = (Page) findValue(pageId);
		if (page == null) {
			page = (Page) ServletActionContext.getRequest()
					.getAttribute(pageId);
		}
		try {
			if (page != null) {
				String html = "";
				if (StringUtil.equals(templetNum, "1")) {
					html = "";
				}else {
					html = getHTML(page);
				}
				pageContext.getOut().write(html);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return SKIP_BODY;
	}

	public String getHTML(Page page) throws TemplateException, IOException {
		Map root = new HashMap();
		root.put("page", page);
		int i = page.getPageSize();
		int t = page.getTotalPages();
		int c = page.getCurrentPage();
		int b = getbegin(c, t);
		int e = getEnd(c, t);
		StringBuffer sb = new StringBuffer();
		
		sb.append("<#setting number_format=\"#.##\"/>");
		sb.append("  <script type='text/javascript' src='/scripts/page_nav.js' charset=utf-8></script>");
		sb.append("<div class=\"lapel right_page\">");
		sb.append("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" align=\"center\">");
		sb.append("<tbody><tr><td width=\"25%\" align=\"left\"> 每页显示&nbsp;");
		sb.append("<a "+ (i == 10 ? "class=\"f_12_white_01\"": "class=\"red_12\"") +" href=\"?${page.getSizeQuery(10)}\">10</a>&nbsp;");
		sb.append("<a "+ (i == 20 ? "class=\"f_12_white_01\"": "class=\"red_12\"") +" href=\"?${page.getSizeQuery(20)}\">20</a>&nbsp;");
		sb.append("<a "+ (i == 50 ? "class=\"f_12_white_01\"": "class=\"red_12\"") +" href=\"?${page.getSizeQuery(50)}\">50</a>&nbsp;");
		sb.append("<a "+ (i == 100 ? "class=\"f_12_white_01\"": "class=\"red_12\"") +" href=\"?${page.getSizeQuery(100)}\">100</a>&nbsp; 条记录&nbsp; </td>");
		sb.append("<td align=\"right\"> 共 ${page.totalPages?string(\"0\")!1}/${page.totalRows?string(\"0\")} 条 第${page.currentPage?string(\"0\")!1}页");
		sb.append("<a href='?${page.getQuery('first')}' class=\"button_01\">首页</a>&nbsp;");
		sb.append("<a href=\"?${page.getQuery('prev')}\" class=\"button_01\">上一页&nbsp;</a>");
		for (; b <= e; b++) {
			sb.append("&nbsp;<a href=\"#\" onclick=\"turn_page(null,${page.currentPage!1}, ${page.pageSize}, ${page.totalRows!0}, \'${page.getQuery('')}\','"+ b+ "')\" "+ (c == b ? "class=\"button_02\"": "class=\"button_03\"")+ ">"+ b+ "</a> ");
		}
		sb.append("<a href='?${page.getQuery('next')}' class=\"button_01\">下一页</a>");
		sb.append("<a href='?${page.getQuery('last')}'  class=\"button_01\">尾页</a>转到: 第");
		sb.append("<input name='text'  id=\"page_nav_number\" type='text' size='3' maxlength='3' /> 页 <a href='#'onclick=\"turn_page('page_nav_number',${page.currentPage!1}, ${page.pageSize}, ${page.totalRows!0}, \'${page.getQuery('')}\')\" class=\"button_01\">GO</a></td></tr>");
		sb.append("</tbody></table></div>");


		return TemplateUtil.getInstance().process("pageNavigate",sb.toString(), root);
	}

	public int getbegin(int c, int t) {
		int b = 1;
		if (c - 2 > 1)
			b = c - 2;
		if (t - (c + 2) < 0) {
			b = b + (t - (c + 2));
		}
		if (b < 1) {
			b = 1;
		}
		return b;
	}

	public int getEnd(int c, int t) {
		int e = t;
		if (e > 5) {
			if (c + 2 < t)
				e = c + 2;
			if (c - 2 <= 0)
				e = e - (c - 2) + 1;
		}
		return e;
	}



	
	public String getTempletNum() {
		return templetNum;
	}

	public void setTempletNum(String templetNum) {
		this.templetNum = templetNum;
	}

}
